#include<bits/stdc++.h>
#define ONLINE_JUDGE
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 410, INF = 0x3f3f3f3f3f3f3f3f;

ll n, m, q, res;
ll mp[N][N];
vector<pair<pair<ll, ll>, ll>> edge;
vector<ll> b;
bool st[10];

void dfs(ll pre, ll cnt, ll sum)
{
    if (cnt == 0) {
        res = min(res, sum + mp[pre][n]);
        return;
    }

    for (ll i = 0;i < (ll)b.size();i++) {
        if (st[i]) continue;
        st[i] = 1;
        auto [tmp, t] = edge[b[i]];
        dfs(tmp.second, cnt - 1, sum + mp[pre][tmp.first] + t);
        dfs(tmp.first, cnt - 1, sum + mp[pre][tmp.second] + t);
        st[i] = 0;
    }
}

void floyd()
{
    for (ll k = 1;k <= n;k++)
        for (ll i = 1;i <= n;i++)
            for (ll j = 1;j <= n;j++)
                mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}

void init()
{
    edge.push_back({ { 0,0 }, 0 });
    for (ll i = 1;i <= n;i++)
        for (ll j = 1;j <= n;j++)
            mp[i][j] = (i == j ? 0 : INF);
}

void solve()
{
    cin >> n >> m;
    init();
    for (ll i = 1;i <= m;i++) {
        ll x, y, t;cin >> x >> y >> t;
        mp[x][y] = mp[y][x] = min(mp[x][y], t);
        edge.push_back({ { x,y }, t });
    }

    floyd();

    cin >> q;
    while (q--) {
        ll k;cin >> k;

        b.clear();
        res = INF;

        for (ll i = 1;i <= k;i++) { ll x; cin >> x; b.push_back(x); };
        dfs(1, k, 0);
        if (res == INF) cout << -1 << '\n';
        else cout << res << '\n';
    }
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    //cin>>T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}